package jdbc.util.CompositeQuery;

import javax.servlet.*;
import javax.servlet.http.*;
import org.json.JSONArray;
import org.json.JSONException;

import com.rider.model.CarRiderPathJsonHandler;

import java.io.*;
import java.util.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.List;
public class ServletAndroidCompositeQuery extends HttpServlet {
	public void doGet(HttpServletRequest rq, HttpServletResponse rp)
			throws ServletException, IOException {
		List<CompositeQueryVO> list = null;
		StringBuffer whereCondition = new StringBuffer();
		Timestamp arrival1 = null;
		String stop_id = rq.getParameter("stop_id");
		String arrival = rq.getParameter("arrival1");
		String arrival2 = rq.getParameter("arrival2");
		String stop_id1 = null;
		String jasonString=null;
		Date today=null;
		Date  today1=null;
		Date todayg=null;
		Date  todayg1=null;
		Date now=null;
		if (stop_id == null || stop_id.length() == 0) {
			stop_id = null;
		}else{
			
			stop_id1 = new String(stop_id.getBytes("ISO-8859-1"), "UTF-8");
		}		
		if (arrival == null || arrival.length() == 0) {
			arrival = null;
		} 
		if (arrival2 == null || arrival2.length() == 0) {
			arrival2 = null;
		} 
		String sql = "";

		if (stop_id != null && stop_id.trim().length() != 0    ) {
			whereCondition.append(" and cs.stop_id=" + "'" + stop_id1 + "'");
		}

		if (arrival != null  && arrival2!=null) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		
			  try {
		            today = df.parse(arrival);
		            today1=  new java.sql.Date(today.getTime());	
		
		            todayg = df.parse(arrival2);
		            todayg1=  new java.sql.Date(todayg.getTime());
		            
		            if (today1.getTime()<todayg1.getTime())
		            {
		            	now=todayg1;
		            	todayg1=today1;
		            	today1=now;
		            }
		      
		        	whereCondition.append(" and csd.arrival between to_date(" +"'" +today1+"',' yyyy-mm-dd')" + " and to_date(" +"'" + (todayg1)+"','yyyy-MM-dd' )");
		        } catch (ParseException e) {
		            e.printStackTrace();
		        }
		
		}

		System.out.println(whereCondition.toString());

		CompositeQueryDAO compositequeryDAO = new CompositeQueryDAO();
		list = compositequeryDAO.getAll(whereCondition);
		
		
		CompositeQueryJsonHandler compositequeryJsonHandler =new CompositeQueryJsonHandler();
		jasonString=compositequeryJsonHandler.toJSONArray(list).toString();


		rp.setContentType("text/html; charset=utf-8");
		PrintWriter out = rp.getWriter();

		out.println(jasonString);
		out.close();
	}

	public void doPost(HttpServletRequest rq, HttpServletResponse rp)
			throws ServletException, IOException {
		List<CompositeQueryVO> list = null;
		StringBuffer whereCondition = new StringBuffer();
		Timestamp arrival1 = null;
		String stop_id = rq.getParameter("stop_id");
		String arrival = rq.getParameter("arrival1");
		String arrival2 = rq.getParameter("arrival2");
		String stop_id1 = null;
		String jasonString=null;
		Date today=null;
		Date  today1=null;
		Date todayg=null;
		Date  todayg1=null;
		Date now=null;
		if (stop_id == null || stop_id.length() == 0) {
			stop_id = null;
		}else{
		
			stop_id1 = new String(stop_id.getBytes("ISO-8859-1"), "UTF-8");
		}		
		if (arrival == null || arrival.length() == 0) {
			arrival = null;
		} 
		if (arrival2 == null || arrival2.length() == 0) {
			arrival2 = null;
		} 
		String sql = "";

		if (stop_id != null && stop_id.trim().length() != 0    ) {
			whereCondition.append(" and cs.stop_id=" + "'" + stop_id1 + "'");
		}

		if (arrival != null  && arrival2!=null) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		
			  try {
		            today = df.parse(arrival);
		            today1=  new java.sql.Date(today.getTime());	
		
		            todayg = df.parse(arrival2);
		            todayg1=  new java.sql.Date(todayg.getTime());
		            
		            if (todayg1.getTime()<today1.getTime())
		            {
		            	now=todayg1;
		            	todayg1=today1;
		            	today1=now;
		            }
		      
		        	whereCondition.append(" and csd.arrival between to_date(" +"'" +today1+"',' yyyy-mm-dd')" + " and to_date(" +"'" + (todayg1)+"','yyyy-MM-dd' )");
		        } catch (ParseException e) {
		            e.printStackTrace();
		        }
		
		}

		System.out.println(whereCondition.toString());

		CompositeQueryDAO compositequeryDAO = new CompositeQueryDAO();
		list = compositequeryDAO.getAll(whereCondition);
		
		
		CompositeQueryJsonHandler compositequeryJsonHandler =new CompositeQueryJsonHandler();
		jasonString=compositequeryJsonHandler.toJSONArray(list).toString();


		rp.setContentType("text/html; charset=utf-8");
		PrintWriter out = rp.getWriter();

		out.println(jasonString);
		out.close();
	}

}